package com.newgrand.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.newgrand.mapper.Pcm3CntChangeDMapper;
import com.newgrand.mapper.Pcm3CntChangeMMapper;
import com.newgrand.mapper.Pcm3CntMMapper;
import com.newgrand.model.Pcm3CntM;
import com.newgrand.utils.StringUtils;
import com.newgrand.utils.i8util.I8ResultUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: zhanglixin
 * @Data: 2023/6/24 20:28
 * @Description: TODO
 */
@Service
public class CntmChangeService {

    @Autowired
    private Pcm3CntChangeMMapper pcm3CntChangeMMapper;

    @Autowired
    private Pcm3CntChangeDMapper pcm3CntChangeDMapper;

    @Autowired
    private Pcm3CntMMapper pcm3CntMMapper;

    @Autowired
    private UipLog uipLog;
    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    public Object Query(String data) {
        try
        {
            JSONObject dataJO = JSONObject.parseObject(data);
            String phid = dataJO.getString("phid");
            if(StringUtils.isEmpty(phid)){
                return I8ResultUtil.error("主键信息不能为空.");
            }
            List<Map<String,Object>> maindataList = pcm3CntChangeMMapper.getData("select * from sec_dev_cntchangem_tc",phid);
            if(maindataList==null||maindataList.size()==0){
                return I8ResultUtil.error("未查询到该单据数据.");
            }
            Map<String,Object> main = maindataList.get(0);
            List<Map<String,Object>> dtldataList = pcm3CntChangeDMapper.getData("select * from sec_dev_cntchanged_tc",phid);
            if(dtldataList!=null&&dtldataList.size()>0){
                main.put("list_data",dtldataList);
            }
            return main;
        }catch (Exception ex){
            return I8ResultUtil.error("接口出现异常:"+ex.getMessage());
        }
    }

    public Object pushMq(String phid) {
        try
        {
            JSONObject push = new JSONObject();
            push.put("phid",phid);

            String message =  push.toJSONString();
            //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
            uipLog.info("MQ","推送合同结算数据",message);
            rabbitTemplate.convertAndSend("pcm3cntpay_ng_exchange", "pcm3cntpay_ng_routing",message);
            return I8ResultUtil.success("推送MQ成功");
        }catch (Exception ex){
            return I8ResultUtil.error("推送MQ出现异常:"+ex.getMessage());
        }
    }

    private static ValueFilter filter = (obj, s, v) -> {
        if (v == null)
            return "";
        return v;
    };
}
